const { ccclass, property } = cc._decorator;
import loadLobby from '../Load/loadLobby';
import WinNodeBase from '../WinNodeBase';
import goodsControl from './goodsControl';
import counterControl from './counterControl';
import DetailsManager from './DetailsManager';
import WinFormBase from '../WinFormBase';
import hitMsgControl from '../Lobby/hitMsgControl';
import SoundMgr from '../SoundMgr';

@ccclass
export default class StoreControl extends WinNodeBase {
    @property(cc.Node)
    content: cc.Node = null;
    @property(cc.Node)
    detailsNode: cc.Node = null;
    @property(cc.Node)
    msgForm: cc.Node = null;
    @property(cc.Node)
    SoundNode: cc.Node = null;
    @property(cc.SpriteFrame)
    FtgIcons: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    ExpIcons: cc.SpriteFrame = null;

    @property(cc.Prefab)
    _GoodsPrefab: cc.Prefab = null
    @property
    _heroIcons: cc.SpriteAtlas = null;
    @property
    _SkillIcons: cc.SpriteAtlas = null;
    @property
    _StoreJson: any = null;
    @property
    _SkillsJson: any = null;
    @property
    _heroDetails: any = null;
    @property
    _counter: counterControl = null;

    _isInit: boolean = false;
    _SoundMgr: SoundMgr = null;

    onLoad() {
        this._SoundMgr = this.SoundNode.getComponent(SoundMgr);
    }

    init() {
        if (this._isInit)
            return;
        super.init();
        //获取常驻节点所绑定的脚本 
        let control = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);
        if (control) {
            this._GoodsPrefab = control.GoodsPfb;
            this._StoreJson = control._StoreJson;
            this._SkillIcons = control.SkillIcons;
            this._heroDetails = control._heroDetails;
            this._heroIcons = control._heroIcons;
            this._SkillsJson = control.SkillsJson;
            this._counter = new counterControl();
            this.loadOther();
            this.loadSkill();
            this.loadHero();
            this._counter.seveCounter();
            this._isInit = true;
        }
    }

    private loadHero() {
        let self = this;
        let heros = gameHelper.getInstance().myHeroDetails;
        this._StoreJson.Hero.forEach(element => {
            let bl: boolean = false;
            for (let index = 0; index < heros.length; index++) {
                const hero = heros[index];
                if (element.Id == hero.Id) {
                    bl = true;
                    break;
                }
            }
            if (!bl) {
                let node = self.getItem(element.Id);
                let control = node.getComponent(goodsControl);
                let date = self.getHeroDetail(self._heroDetails.Heros, element.Id);
                if (date) {
                    let counter = self._counter.getCounterItem(element.Xgtype, element.Id);
                    if (!counter) {
                        counter = { id: element.Id, count: element.Xgnumber, used: 0 };
                        self._counter.setCounterItem(element.Xgtype, counter);
                    }
                    if (counter.count > counter.used) {
                        control.goodSprite.spriteFrame = self._heroIcons.getSpriteFrame(date.Details.Sptite);
                        control.nameLabel.string = date.Details.Name;
                        control.describe.string = date.Details.Explain;
                        control.money.string = element.Price;
                        control.xgCount = element.Xgnumber;
                        control.xgUsed = counter.used;
                        control.xgType = element.Xgtype;
                        control.size.string = element.Number;
                        control.xgSize = element.Number;
                        if (element.Price > 0) {
                            control.buy.node.active = true;
                            control.buy.clickEvents.push(this.setBtClick({ price: element.Price, target: node }));
                        }
                        else {
                            control.ads.node.active = true;
                            //广告播放
                        }
                        control._Key = "hero";
                        control._Id = element.Id;
                        if (!node.parent)
                            self.content.addChild(node);
                    }
                }
            }
        });
    }

    private loadSkill() {
        let self = this;
        this._StoreJson.Skill.forEach(element => {
            let node = self.getItem(element.Id);
            let control = node.getComponent(goodsControl);
            let date = self.getSkillDetail(self._SkillsJson.SkillHs, element.Id);
            if (date) {
                let counter = self._counter.getCounterItem(element.Xgtype, element.Id);
                if (!counter) {
                    counter = { id: element.Id, count: element.Xgnumber, used: 0 };
                    self._counter.setCounterItem(element.Xgtype, counter);
                }
                if (counter.count > counter.used) {
                    control.goodSprite.spriteFrame = self._SkillIcons.getSpriteFrame(date.Icon);
                    control.nameLabel.string = date.Title;
                    control.describe.string = date.Explain;
                    control.money.string = element.Price;
                    control.xgCount = element.Xgnumber;
                    control.xgUsed = counter.used;
                    control.xgType = element.Xgtype;
                    control.size.string = element.Number;
                    control.xgSize = element.Number;
                    if (element.Price > 0) {
                        control.buy.node.active = true;
                        control.buy.clickEvents.push(this.setBtClick({ price: element.Price, target: node }));
                    }
                    else {
                        control.ads.node.active = true;
                        //广告播放
                    }
                    control._Key = "skill";
                    control._Id = element.Id;
                    if (!node.parent)
                        self.content.addChild(node);
                }
            }
        });
    }

    private loadOther() {
        let self = this;
        this._StoreJson.Other.forEach(element => {
            let node = self.getItem(element.Id);
            let control = node.getComponent(goodsControl);
            if (element.Id == "ftg") {
                let counter = self._counter.getCounterItem(element.Xgtype, element.Id);
                if (!counter) {
                    counter = { id: element.Id, count: element.Xgnumber, used: 0 };
                    self._counter.setCounterItem(element.Xgtype, counter);
                }
                if (counter.count > counter.used) {
                    control.goodSprite.spriteFrame = self.FtgIcons;
                    control.nameLabel.string = "体力值";
                    control.describe.string = "每开始一局游戏都需要消耗响应数量的体力值";
                    control.money.string = element.Price;
                    control.xgCount = element.Xgnumber;
                    control.xgUsed = counter.used;
                    control.xgType = element.Xgtype;
                    control.size.string = element.Number;
                    control.xgSize = element.Number;
                    if (element.Price > 0) {
                        control.buy.node.active = true;
                        control.buy.clickEvents.push(this.setBtClick({ price: element.Price, target: node }));
                    }
                    else {
                        control.ads.node.active = true;
                        //广告播放
                    }
                    control._Key = "other";
                    control._Id = element.Id;
                    if (!node.parent)
                        self.content.addChild(node);
                }
            }
            else if (element.Id == "exp") {
                let counter = self._counter.getCounterItem(element.Xgtype, element.Id);
                if (!counter) {
                    counter = { id: element.Id, count: element.Xgnumber, used: 0 };
                    self._counter.setCounterItem(element.Xgtype, counter);
                }
                if (counter.count > counter.used) {
                    control.goodSprite.spriteFrame = self.ExpIcons;
                    control.nameLabel.string = "经验值";
                    control.describe.string = "用于升级你的宠物";
                    control.money.string = element.Price;
                    control.xgCount = element.Xgnumber;
                    control.xgUsed = counter.used;
                    control.xgType = element.Xgtype;
                    control.size.string = element.Number;
                    control.xgSize = element.Number;
                    if (element.Price > 0) {
                        control.buy.node.active = true;
                        control.buy.clickEvents.push(this.setBtClick({ price: element.Price, target: node }));
                    }
                    else {
                        control.ads.node.active = true;
                        //广告播放
                    }
                    control._Key = "other";
                    control._Id = element.Id;
                    if (!node.parent)
                        self.content.addChild(node);
                }
            }
        });
    }

    private setBtClick(date: any): cc.Component.EventHandler {
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node;
        clickEventHandler.component = "StoreControl";
        clickEventHandler.handler = "buy";
        clickEventHandler.customEventData = date;
        return clickEventHandler;
    }

    getSkillDetail(list: any, id: string): any {
        for (let index = 0; index < list.Hit.length; index++) {
            const element = list.Hit[index];
            if (element.Id == id) {
                return element;
            }
        }
        for (let index = 0; index < list.Put.length; index++) {
            const element = list.Put[index];
            if (element.Id == id) {
                return element;
            }
        }
        for (let index = 0; index < list.Self.length; index++) {
            const element = list.Self[index];
            if (element.Id == id) {
                return element;
            }
        }
        return null;
    }

    getHeroDetail(list: any, id: string): any {
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            if (element.Id == id) {
                return element;
            }
        }
        return null;
    }

    getItem(id: string): cc.Node {
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            if (element.name == id) {
                return element;
            }
        }
        let node = cc.instantiate(this._GoodsPrefab);
        node.name = id;
        return node;
    }

    buy(event, customEventData) {
        let control: goodsControl = customEventData.target.getComponent(goodsControl);
        let Details = this.detailsNode.getComponent(DetailsManager);
        if (Details.disManey(customEventData.price) < 0) {
            this.showMsg("客官，您的账户余额不足");
        }
        else {
            if ("hero" == control._Key) {
                Details.addHeroScrol(control._Id);
            }
            else if ("skill" == control._Key) {
                let date = gameHelper.getInstance().skillParam;
                for (let ii = 0; ii < control.xgSize; ii++)
                    date.b.push(control._Id);
                date.new = true;
                gameHelper.getInstance().skillParam = date;
            }
            else {
                if (control._Id == "ftg") {
                    Details.addFtg(control.xgSize);
                }
                else if (control._Id == "exp") {
                    Details.addExp(control.xgSize);
                }
            }
            this._SoundMgr.play("u_shoping");
            this.updateConuter(control);
            this.showMsg("购买成功");
        }
    }

    updateConuter(goods: goodsControl) {
        if (!this._counter)
            this._counter = new counterControl();
        let item = this._counter.getCounterItem(goods.xgType, goods._Id);
        item.used++;
        goods.xgUsed++;
        this._counter.setCounterItem(goods.xgType, item);
        goods.showXg();
    }
    showMsg(msg: string) {
        let control = this.msgForm.getComponent(hitMsgControl)
        control.lebelNode.string = msg;
        let formNode = new cc.Node();
        let formControl = formNode.addComponent(WinFormBase);
        formControl.formIndex = 2;
        formControl.bgNode = this.FormBg;
        formControl.mainNode = this.msgForm;
        this.node.addChild(formNode);
    }
    onDisable() {
        this._counter.seveCounter();
    }
}
